<!--
 * @description 组件
 * @author: 陈陈
 * @date: 2025/05/09
 * @phone: 18560000860
 * @email: 18560000860@qq.com
 * @company: 济南晨霜信息技术有限公司 /
 * @business: 承接前后端项目的开发
-->
<template>
	<view
		class="pure-amount"
		:class="_classes"
		:style="_styles"
		@tap="_handleClick"
	>
		<!-- 货币符号 -->
		<view
			class="pure-amount__symbol"
			v-if="symbol"
			>{{ symbol }}</view
		>

		<!-- 整数部分 -->
		<view class="pure-amount__int">{{ _int }}</view>

		<!-- 小数点 -->
		<view
			class="pure-amount__dot"
			v-if="_float !== ''"
			>.</view
		>

		<!-- 小数部分 -->
		<view
			class="pure-amount__float"
			v-if="_float !== ''"
			>{{ _float }}</view
		>
	</view>
</template>

<script>
	import commonComponentOptions from "../../config/options";
	export default {
		options: commonComponentOptions
	};
</script>

<script setup>
	import { computed } from "vue";
	import props from "./props";

	// -------------------------------------------------------------------------------- Props
	const _props = defineProps(props);

	// -------------------------------------------------------------------------------- Emits
	const _emits = defineEmits(["onClick"]);

	// -------------------------------------------------------------------------------- Classes
	const _classes = computed(() => {
		const array = [];

		// 大小模式
		if (_props?.sameNumber) array.push("pure-amount--mode--same-number");
		if (_props?.sameAll) array.push("pure-amount--mode--same-all");

		return array;
	});

	// -------------------------------------------------------------------------------- Styles
	const _styles = computed(() => {
		const array = [];
		return array;
	});

	// 整数部分
	const _int = computed(() => {
		const num = _props.amount?.toString?.()?.split?.(".")?.[0] || "0";

		// 增加千分位符号
		if (_props.thousands) return num?.replace?.(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") || "";

		// 不用增加千分位符号
		return num;
	});

	// 小数部分
	const _float = computed(() => {
		let num = _props.amount?.toString?.()?.split?.(".")?.[1] || "";

		// 小数点后的位数小于精度
		if (num.length <= _props.precision) {
			// 不需要补零
			// num = num;

			// 需要补0
			if (num === "") num = "0";
			num = num.padEnd(_props.precision, "0");
		}

		// 小数点后的位数大于精度
		else {
			// 舍弃指定精度后面的位数
			num = num.slice(0, _props.precision);
		}

		// 开启了舍弃末尾0的选项
		if (_props.dropZero) {
			num = num.replace(/0+$/, "");
		}

		return num;
	});

	// 点击事件
	function _handleClick() {
		_emits("onClick");
	}
</script>

<style scoped lang="scss">
	@import "./style.scss";
</style>
